/**
 * https://leetcode-cn.com/problems/contains-duplicate/
 * 存在重复元素
 * @param nums 
 */
const containsDuplicate = (nums: number[]): boolean => {
    // 1. 排序
    nums.sort((a, b) => a - b);

    // 2. 遍历
    let curr = nums[0],
        len = nums.length;

    for (let i = 1; i < len; i++) {
        let v = nums[i]

        // 相等, 说明有多于两个的
        if (v === curr) return true

        // 不相等
        curr = v
    }

    return false
};

const containsDuplicate2 = (nums: number[]): boolean => {
    const map: Record<number, number> = {}

    const len = nums.length;

    // 遍历
    for (let i = 0; i < len; i++) {
        // 没有默认为0
        map[nums[i]] = map[nums[i]] ?? 0;

        // 如果次数多于1个
        if (++map[nums[i]] === 2) return true;
    }

    return false
};